package com.youmingxing.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.youmingxing.R;
import com.youmingxing.bean.Web;
import com.youmingxing.network.HttpManager;


/**
 * JHS
 * Edited By ChenZhixi
 *
 * @since 2015-9-14
 * WebView 页面
 */

public class WebViewActivity extends BaseActivity {

    WebView webView;
    String url = "";
    public static final String EXTRA_WEB_PAGE = "web_page";
    public static final String EXTRAS_URL = "url";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_webview);
        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        Bundle bundle = getIntent().getExtras();
        url = bundle.getString(EXTRAS_URL);
        initView((Web) bundle.getSerializable(EXTRA_WEB_PAGE));
    }

    private void initView(Web page) {
        switch (page) {
            case popularity:
                setTopbarName(R.string.popularity_title_method);
                webView.loadUrl(HttpManager.BASE_URL_notS + "popularity.html");
                break;
            case licence:
                setTopbarName(R.string.agreement_title);
                webView.loadUrl(HttpManager.BASE_URL_notS + "license.html");
                break;
            case audition:
                setTopbarName("全民造星海选协议");
                webView.loadUrl(HttpManager.BASE_URL_notS + "audition.html");
                break;
            case tickets:
                setTopbarName(R.string.mytickets_method_title);
                break;
            case pages:
                webView.loadUrl(url);
                WebChromeClient webChromeClient = new WebChromeClient() {
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        setTopbarName(title);

                    }
                };
                webView.setWebChromeClient(webChromeClient);
                webView.setWebViewClient(new MyWebViewClient());
                break;
        }
    }

    class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("http://")) {
                view.loadUrl(url);
            } else if (url.contains("allstar://")) {
                Intent intent = new Intent(new Intent(WebViewActivity.this, MainActivity.class));
                Uri uri = Uri.parse(url);
                intent.setDataAndType(uri, "text/plain");
                startActivity(intent);
            } else {

            }
            return true;
        }

    }



}
